// <copyright file="LocalDataSource.cs" company="YUIAsp.Net">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>raul popescu</author>
// <email>dev@yuiasp.net</email>
// <date>2009-01-30</date>
// <summary>YUIAsp.Net File</summary>

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace YUIAspNet
{
    /// <summary>
    /// Represents a local data source.
    /// The data defined through this control will be rendered with the control and be available immediately after the page's load.
    /// </summary>
    public class LocalDataSource : DataSourceBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LocalDataSource"/> class.
        /// </summary>
        public LocalDataSource() : base(DataSourceType.Local)
        {
        }

        /// <summary>
        /// Adds the column.
        /// </summary>
        /// <param name="columnName">Name of the column.</param>
        public void AddColumn(string columnName)
        {
            Keys.Add(columnName);
        }

        /// <summary>
        /// Adds the row.
        /// </summary>
        /// <param name="items">The items.</param>
        public void AddRow(params object[] items)
        {
            Values.Add(items);
        }

        /// <summary>
        /// Gets the array of <see cref="System.Object"/> with the specified items.
        /// </summary>
        /// <value></value>
        public object[] this[string item]
        {
            get
            {
                int colIdx = Keys.IndexOf(item);
                if (colIdx < 0)
                    throw new ArgumentException(item);
                return Values[colIdx];
            }
        }

        /// <summary>
        /// Adds the setting.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public void AddSetting(string key, object value)
        {
            AdditionalItems.Add(key, value);
        }

        /// <summary>
        /// Gets the count.
        /// </summary>
        /// <value>The count.</value>
        public int Count
        {
            get { return this.Values.Count; }
        }

        /// <summary>
        /// Serializes the specific properties foreach implementor.
        /// </summary>
        /// <returns></returns>
        protected override StringBuilder SerializeSpecificData()
        {
            StringBuilder sbJson = new StringBuilder();
            sbJson.Append("\"Data\": [");
            bool first = true;
            foreach (object[] row in Values)
            {
                sbJson.AppendFormat(CultureInfo.InvariantCulture, "{0}{{", first ? string.Empty : ",");

                int colIdx = 0;
                foreach (object item in row)
                {
                    if (colIdx > 0) sbJson.Append(",");

                    sbJson.AppendFormat(CultureInfo.InvariantCulture, "\"{0}\":{1}", Keys[colIdx], FormatValue(item));

                    colIdx++;
                }

                first = false;

                sbJson.Append("}");
            }

            sbJson.Append(" ]");

            if (AdditionalItems != null && AdditionalItems.Keys.Count > 0)
            {
                //sbJson.Append(",Meta: { ");
                //first = true;
                foreach (KeyValuePair<string, object> additionalItem in AdditionalItems)
                {
                    sbJson.AppendFormat(CultureInfo.InvariantCulture, "{0}\"{1}\": {2}", first ? string.Empty : ",",
                                        additionalItem.Key, FormatValue(additionalItem.Value));
                    first = false;
                }

                //sbJson.Append(" }");
            }
            return sbJson;
        }
    }
}
